<template>
  <BasicModal v-bind="$attrs" @register="userRegister" :title="getTitle" width="850px" @ok="submitAudit">
    <BasicTable @register="registerUserTable" :loading="loading" :pagination="{ pageSize: 20 }" :rowSelection="{ type: 'radio' }"> 
      
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'collect'">
            <a  v-if="record.collect==='0'" @click="handleCollect(record)"><HeartOutlined  style="margin-right:8px;font-size:large"/></a>
            <a style="color:orange" v-if="record.collect==='1'" @click="handleCollect(record)"><HeartFilled  style="margin-right:8px;font-size:large"/></a>
          </template>
        </template>
      <template #action="{record }">       
        <TableAction :actions="getTableAction(record)" />        
      </template>
    </BasicTable>
  </BasicModal>
  <TableModal @register="registerItemsModal" @success="handleSuccess"></TableModal>
</template>

<script lang="ts" setup>
  import { BasicModal, useModalInner,useModal } from '/@/components/Modal';
  import { ref, reactive } from 'vue';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { getTempList,addCollect } from '../LabWscientificItem.api';
  import { labTempColumns } from '../LabWscientificItem.data';
  import { BasicTable,TableAction } from '/@/components/Table';
  import { FormSchema } from '/@/components/Table';
  import TableModal from '/@/views/lab/basic/items/components/LabCheckItemsFrom.vue';
  import { useUserStore } from '/@/store/modules/user';
  import { HeartOutlined,HeartFilled } from '@ant-design/icons-vue';

  const { userInfo } = useUserStore();

  const loading = ref(false);
  const getTitle = ref('实验项目模板');

  const formPerData = reactive<Record<string, any>>({
    oper: '',
    name: '',
  });

  let collectState = ref('');
  const emits = defineEmits<{
    (event: 'getRatifyResultS2', uId: String, uName: String): void;
  }>();

  const [userRegister, { setModalProps, closeModal }] = useModalInner(async (data) => {
    setModalProps({
      minHeight: 180,
    });
    loading.value = true;
    formPerData.oper = data.oper;
    formPerData.name = data.name;
  });

  const searchFormSchema: FormSchema[] = [
    {
      label: '项目名称',
      field: 'formPerData.name',
      component: 'Input',
      colProps: { span: 6},
    },
  ];

  // 注册table数据
  const { tableContext } = useListPage({
    tableProps: {
      api: getTempList,
      columns: labTempColumns,
      canResize: false,
      formConfig: {
        schemas: searchFormSchema,
      },
      showIndexColumn: true,
      showTableSetting: false,
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        if(params.formPerData){
          params.name2 = params.formPerData.name;
          return Object.assign(params, {nameswa:params.name2 });
        }else {
          return Object.assign(params);
        };
      },
    },
  });

  const [registerUserTable, { reload ,getSelectRows}, { selectedRowKeys }] = tableContext;

  async function submitAudit() {
    const selectedRows = getSelectRows()[0];
    const id = selectedRows['id'];
    const name = selectedRows['name'];
    closeModal();
    emits('getRatifyResultS2', id, name);

  }
  const [registerItemsModal, { openModal }] = useModal();

   /**
   * 操作栏
   */
   function getTableAction(record) {
    return [
      {
        label: '模板',
        onClick: openTemp.bind(null, record),
      }
    ];
  }
  async function handleCollect(record){
    // console.log('点击收藏',record);
    // let 
    if(record.collect === '0'){
      collectState.value='1';
    }else{
      collectState.value='0';
    }
    // console.log('userInfo',userInfo)
    let params = {
      id: record.id,
      userId: userInfo.id,
      collect:collectState.value,
    }
    // console.log('params',params)
    const result = await addCollect(params);
    // console.log('result',result);
    if(result.code === 200){
      reload();
    }
  }
  function openTemp(record) {
  //  console.log(record.id);
    openModal(true, {
      record,
      disabled: false,
    });
  }
  /**
   * 成功回调
   */
   function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

</script>
<style scoped lang="less">
  ::v-deep .jeecg-basic-form .ant-form-item-label > label {
    font-size: 14px;
  }
  ::v-deep .jeecg-basic-form .ant-input {
    font-size: 14px;
  }
</style>
